/**
 * Created by fupeng on 17/2/16.
 */
var pool = require('./DBConnectPool');
var logger = require('../../common/Logger/Logger');


module.exports = {
    getMovieInfo: function (param, _param_page, callBack) {
        pool.getPoolConnection('mtsc', function (err, connection) {
            if (err) {
                console.log('Error: get connection from pool in getMovieInfo: ', err);
                return;
            }
            var sql = 'SELECT movie.poster,movie.mid,movie.movie_name,movie.director,movie.brief,movie.cast,movie.score,movie.country,movie.category,date_format(movie.publishdate, "%Y") as publishdate,movie.movie_bigpic ' +
                'from weixin_package_detail movie ' +
                'where movie.cinema_code = ? and movie.isdeleted != 1   LIMIT ?,10';

            connection.query(sql, [param,_param_page*10], function (queryErr, rows) {
                if (queryErr) {
                    console.log('影吧片库 影片查询 失败', queryErr);

                    callBack({
                        isSuccess: false
                    });

                } else {

                    callBack({
                        isSuccess: true,
                        rows: rows
                    })

                }

                connection.release();

            })
        });
    },
    /**
     * 影吧片库--影片搜索
     * @param Param
     * @param callBack
     */
    getMovieInfoByParam: function (param, callBack) {
        pool.getPoolConnection('mtsc', function (err, connection) {
            if (err) {

                console.log('Error: get connection from pool in getMovieInfoByParam: ', err);
                return;
            }
            var sql = 'SELECT movietp.* from (SELECT movie.poster,movie.mid,movie.movie_name,movie.director,movie.brief,movie.cast,movie.score,movie.country,movie.category,date_format(movie.publishdate, "%Y") as publishdate,movie.movie_bigpic ' +
                ' from weixin_package_detail movie '+
                ' where movie.cinema_code = ' + param.cinemaCode + param.movieName  + param.country + param.publishdate + ' and movie.isdeleted != 1 ) '+
                ' as movietp ';

            if(param.category != 0){
                sql += ' LEFT JOIN movietypes on movietp.mid = movietypes.movie_id '+
                    ' LEFT JOIN movie_type on movietypes.type_id = movie_type.id '+
                    ' '+param.category +' group by movietp.mid '+param.sort+' limit ?,10';
            }else{
                sql += ' group by movietp.mid '+param.sort+' limit ?,10';
            }

            connection.query(sql,[param.page], function (queryErr, rows) {

                if (queryErr) {

                    console.log('搜索影吧片库有误: ', queryErr);

                    callBack({
                        isSuccess: false
                    });

                } else {

                    callBack({
                        isSuccess: true,
                        rows: rows
                    })

                }

                connection.release();

            })

        });
    }

};